using Script.CreatureEditor;
using UnityEngine;
using UnityEngine.UI;

public class ColorPalette : MonoBehaviour
{
	public EditorController editorController;

	private int colorId;

	private Color kolor = Color.red;

	private void Start()
	{
		PreparePalette();
	}

	private void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			ChooseColor();
		}
	}

	public void ChangeColor(Image obrazek)
	{
		kolor = obrazek.color;
		Transform child = base.transform.GetChild(0);
		for (int i = 1; i < 18; i++)
		{
			Image component = child.GetChild(i).GetComponent<Image>();
			component.color = new Color(kolor.r, kolor.g, kolor.b, component.color.a);
		}
	}

	private void ChooseColor()
	{
		Vector3 vector = WskazanyPunkt() * 1.1111112f;
		float num = 4.68f;
		float wartosc = 1f / 18f;
		if (!(vector.x < 0f - num) && !(vector.x > num) && !(vector.y < 0f - num) && !(vector.y > num))
		{
			float num2 = 1f - Zaokraglanie((vector.x + num) / (num * 2f), wartosc);
			float num3 = Zaokraglanie((vector.y + num) / (num * 2f), wartosc);
			byte[] array = new byte[3]
			{
				(byte)(kolor.r * 255f),
				(byte)(kolor.g * 255f),
				(byte)(kolor.b * 255f)
			};
			for (int i = 0; i < 3; i++)
			{
				byte b = (byte)(255 - array[i]);
				array[i] = (byte)(((float)(int)array[i] + (float)(int)b * num2) * num3);
			}
			Color32 choosenColor = new Color32(array[0], array[1], array[2], byte.MaxValue);
			editorController.ChooseColor(colorId, choosenColor);
			Hide();
		}
	}

	private void Hide()
	{
		base.gameObject.SetActive(false);
		editorController.enabled = true;
	}

	private void PreparePalette()
	{
		Transform child = base.transform.GetChild(1);
		GameObject gameObject = child.GetChild(0).gameObject;
		byte[] array = new byte[3] { 255, 0, 0 };
		float num = 0.0917647f;
		Vector2 vector = new Vector2(0f, -4.68f + num * 0.5f);
		for (int i = 0; i < 6; i++)
		{
			for (int j = 0; j < 17; j++)
			{
				Transform transform = Object.Instantiate(gameObject, Vector2.zero, Quaternion.identity).transform;
				transform.SetParent(child);
				transform.localScale = Vector2.one;
				transform.localPosition = vector + new Vector2(0f, (float)i * num * 17f + (float)j * num);
				transform.name = "Kolor";
				switch (i)
				{
				case 0:
					array[1] += 15;
					break;
				case 1:
					array[0] -= 15;
					break;
				case 2:
					array[2] += 15;
					break;
				case 3:
					array[1] -= 15;
					break;
				case 4:
					array[0] += 15;
					break;
				case 5:
					array[2] -= 15;
					break;
				}
				transform.GetComponent<Image>().color = new Color32(array[0], array[1], array[2], byte.MaxValue);
			}
		}
		Object.Destroy(gameObject);
	}

	public void Show(int colorId)
	{
		this.colorId = colorId;
		base.gameObject.SetActive(true);
		editorController.enabled = false;
	}

	private Vector3 WskazanyPunkt()
	{
		Vector3 mousePosition = Input.mousePosition;
		mousePosition.z = -10f;
		Vector3 vector = Camera.main.ScreenToWorldPoint(mousePosition);
		return new Vector3(vector.x, vector.y, 0f);
	}

	private float Zaokraglanie(float liczba, float wartosc)
	{
		float num = 0f;
		while (liczba >= wartosc)
		{
			liczba -= wartosc;
			num += 1f;
		}
		return num / 17f;
	}
}
